﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.Util;
using Com.CMS263.PO;
namespace Com.CMS263.BLL
{
    public class ArtPageTransform :ITag
    {
         /// <summary>
        /// 要解析的含有标签的html代码
        /// </summary>
        private string ctx = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ctx">页面内容</param>
        /// 
        List<string> tags = new List<string>();
        List<string> tags2 = new List<string>();
        TagUtil tu = new TagUtil();
        ArtBase ab = new ArtBase();
        ArtBaseBLL bll = new ArtBaseBLL();
        ExtValueBLL evbll = new ExtValueBLL();
        int rid = 0;
        //public ArtPageTransform(string ctx)
        //{
        //    this.ctx = ctx;
        //}
        /// <summary>
        /// 得到替换标签后的内容
        /// </summary>
        /// <returns>返回替换后的结果</returns>
        /// 
        int ta = 0;
        public string Replace(string ctx)
        {
            try
            {
                ta = tu.GetTag(ctx, "cms:singlepage", tags).Count;
            }
            catch { }
            //得到所有标签
            if (ta > 0)
            {
                tags = tu.GetTag(ctx, "cms:singlepage", tags);
                foreach (string tag in tags)
                {
                    string innnerListHTML = tu.GetInnerHTML("cms:singlepage", tag);
                    /// <summary>
                    /// 文章分类编号
                    /// </summary>
                    int menuID = 0;
                    /// <summary>
                    /// 取得编号的request参数的名字
                    /// </summary>
                    string paraName = "";
                    //得到包含属性的标签字符串
                    int end = tag.IndexOf(">");
                    string arrtStr = tag.Substring(1, end - 1);
                    //标签呗替换后生成的html
                    string tagInnerHTML = "";
                    //解析标签得到所有的属性
                    bool f = true;
                    Dictionary<string, string> attrDic = tu.Getattribute(arrtStr);
                    try
                    {
                        f = string.IsNullOrEmpty(attrDic["menuid"]);
                    }
                    catch { }
                    if (f == false)
                    {
                            string str = attrDic["menuid"].Replace("\"", "");
                            menuID = int.Parse(str);
                           
                    }
                    else
                    {
                        //如果是通过传递过来的参数取分类的那么就用参数得到编号
                        string para = System.Web.HttpContext.Current.Request[attrDic["paraname"].Replace("\"", "")];
                        if (!string.IsNullOrEmpty(para))
                        {
                            menuID = int.Parse(para);
                        }
                        else
                        {
                            throw new Exception("分类参数名称错误，请使用正确的参数名称传递参数");
                        }
                    }

                        //根据menuid获取对象
                        ab = bll.GetArtBaseByMenuId(menuID);
                        string alllist = "";
                        //解析里面的<cms:attribute  field=""  length=""/>标签并用一条数据中的各个属性替换标签
                        List<string> attrs = new List<string>();
                        attrs = tu.GetTag(tag, "cms:attribute", attrs);

                        //循环累加的连接字符串
                        string innerHTML = tu.GetInnerHTML("cms:singlepage", tag);
                        foreach (string attr in attrs)
                        {

                            Dictionary<string, string> attrlist = tu.Getattribute(attr);
                            string attrvalue = "";
                            int filedID = 0;
                            string field = attrlist["field"].Replace("\"", "").ToLower();
                            int length = 0;

                            if (field.ToLower() == "title")
                            {
                                attrvalue = ab.title.ToString();
                            }
                            else if (field.ToLower() == "adddate")
                            {
                                string v = "";
                                attrvalue = ab.addDate.ToString();

                                if (attrlist.TryGetValue("formate", out v))
                                {
                                    v = attrlist["formate"].Replace("\"", "");
                                    attrvalue = ab.addDate.ToString(v);
                                }
                            }
                            else if (field.ToLower() == "menuid")
                            {
                                attrvalue = ab.menuId.ToString();
                            }
                            else if (field.ToLower() == "id")
                            {
                                attrvalue = ab.id.ToString();
                            }

                            else
                            {
                                //当是属性编号时，将属性编号变成整形，否则报错
                                try
                                {
                                    filedID = int.Parse(field);
                                    attrvalue = evbll.GetValueById(ab.id, filedID);
                                }
                                catch
                                {
                                    throw new Exception("属性值错误，请检查属性值：" + field);
                                }

                            }

                            //如果有长度要求，那么进行截取
                            if (!string.IsNullOrEmpty(attrlist["length"]))
                            {
                                string l = attrlist["length"].Replace("\"", "");
                                if (!string.IsNullOrEmpty(l))
                                {
                                    length = Convert.ToInt32(l);
                                    attrvalue = StringUtil.GetText(attrvalue, length);
                                }
                            }
                            innerHTML = innerHTML.Replace(attr, attrvalue);
                        }
                        alllist += innerHTML;
                        ctx = ctx.Replace(tag, alllist);
                    //}
                   
                }
                //判断如果标签<cms:singlepagebyid存在，则执行ArtPageTransformbyid
                     if (tu.GetTag(ctx, "cms:onepagebyid", tags).Count > 0)
                    {
                        ArtPageTransformbyid aptid = new ArtPageTransformbyid();
                        ctx = aptid.Replace(ctx);
                        return ctx;
                    }
                    else
                    {
                        //throw new Exception("分类参数名称错误，请使用正确的参数名称传递参数");
                        return ctx;
                    }     

            }
            else
            {
                try
                {
                    ta = tu.GetTag(ctx, "cms:onepagebyid", tags).Count;
                }
                catch { }
                if (ta > 0)
                {
                    ArtPageTransformbyid aptid = new ArtPageTransformbyid();
                    ctx = aptid.Replace(ctx);
                    return ctx;
                    
                }
                else
                {
                    return ctx;
                }
            }
            
        }
    }
}
